﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ScanTemplate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSelectdir_Click(object sender, EventArgs e)
        {
            FormSelectUnScanDir f = new FormSelectUnScanDir();
            f.ShowDialog();
            if (f.SelectedDir != null)
            {
                _ActiveDir = f.SelectedDir;
                string dirname = _ActiveDir.FullPath;
                if (dirname.Contains(""))
                    dirname = dirname.Substring(dirname.LastIndexOf("\\"));
                string msg = "文件夹名:" + dirname + " 共有文档：" + _ActiveDir.SubListfiles.Count + " 份\r\n";
                msg += string.Join("\r\n", _ActiveDir.SubListfiles.Take(5)
                    .Select(r => r.FileName).ToList()) + "\r\n";

                if (_ActiveDir.ABPage)
                    msg += "  存在正反面";
                else
                    msg += "  不存在正反面";
                textBoxDir.Text = msg;
                //_idbe.B = 0;
                //_idbe.E = _ActiveDir.SubListfiles.Count;
                //_idbe.MoveToTop();
                //EnableButtons();
                //ShowImages();
            }
            else
            {
            }
        }
        private void FormChoose_Load(object sender, EventArgs e)
        {
            if (File.Exists("global.json"))
            {
                try
                {
                    string _filename = "global.json";
                    globalsave gs = Newtonsoft.Json.JsonConvert.DeserializeObject<globalsave>(File.ReadAllText(_filename));
                    global.Debug = gs.Debug;
                    global.featuretype = gs.featuretype;
                    global.tag = gs.tag;
                    global.msg = gs.msg;
                    global.UserMode = true;
                    if (gs.ProgramerCode == "ljh_beer@163.com.copyright")
                    {
                        global.UserMode = false;
                    }
                }
                catch
                {
                }
            }
            else
            {
                SaveGlobal();
            }           
            global.ABPage = false;

            if (!File.Exists("scanpaper_config.json"))
                ReModiOrCreateConfig();
            else
            {
                JHConfig.JSConfig jsconfig = new JHConfig.JSConfig();
                jsconfig.Load("scanpaper_config.json");
                if (!jsconfig.pathconfig.ExistDirectory())
                    ReModiOrCreateConfig();
                else
                    global.Jsconfig = jsconfig;
            }
            if (global.Jsconfig.globalconfig.Baseconfig == null)
                global.Jsconfig.globalconfig.InitBaseconfig();
            
        }
        private void ReModiOrCreateConfig()
        {
            JHConfig.JSConfig jsconfig = new JHConfig.JSConfig();
            //FormSetPath f = new FormSetPath(jsconfig);
            //f.ShowDialog();
            //if (f.IsOK())
            {
                jsconfig.Save("scanpaper_config.json");
                global.Jsconfig = jsconfig;
            }
            //else
            //{
            //    MessageBox.Show("路径设置不正确");
            //    this.Close();
            //}
        }
        private void FormChoose_FormClosing(object sender, FormClosingEventArgs e)
        {          
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();
            this.Close();
        }

        private void SaveGlobal()
        {
            //string str = Tools.JsonFormatTool.ConvertJsonString(Newtonsoft.Json.JsonConvert.SerializeObject(new  global()));
            //File.WriteAllText("global.json", str);
            globalsave gs = new globalsave();
            gs.Debug = global.Debug;
            gs.featuretype = global.featuretype;
            gs.tag = global.tag;
            gs.msg = global.msg;
            string str = Tools.JsonFormatTool.ConvertJsonString(Newtonsoft.Json.JsonConvert.SerializeObject(gs));
            File.WriteAllText("global.json", str);
        }
        public Tools.FileDir _ActiveDir { get; set; }
    }
}
